package com.time.company.components.popup;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.time.company.R;
import com.time.company.app.MyApplication;
import com.time.company.base.BasePW;


/**
 * 选择头像的PW
 * Author: Created by deng on 2016/8/16.
 * E-mail: tx16bing@163.com
 */
public class ImagePW extends BasePW implements View.OnClickListener {

    private Context mContext;
    private int mType;

    public ImagePW(Context context, ViewGroup view) {
        // 默认头像类型
        this(context, view, MyApplication.getInstance().getResources().getInteger(R.integer.pw_type_head));
    }

    public ImagePW(Context context, ViewGroup view, int type){
        this.mContext = context;
        this.mType = type;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mView = inflater.inflate(R.layout.pw_select_path, view, false);
        assignViews(mView);
        setContentView(mView);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        setOutsideTouchable(true);
        this.setAnimationStyle(R.style.Pw_Bottom_Animation);
        ColorDrawable dw = new ColorDrawable(0x80000000);
        this.setBackgroundDrawable(dw);
    }

    private boolean isHead(){
        if (mType == mContext.getResources().getInteger(R.integer.pw_type_head)){
            return true;
        }

        return false;
    }

    private void assignViews(View mView) {
        TextView tvOne = (TextView) mView.findViewById(R.id.tv_one);
        TextView tvTwo = (TextView) mView.findViewById(R.id.tv_two);
        TextView tvCancel = (TextView) mView.findViewById(R.id.tv_path_cancel);

        if (isHead()){
            tvOne.setText("拍照");
            tvTwo.setText("相册选取");
            tvTwo.setTextColor(ContextCompat.getColor(mContext, R.color.colorThirdPrimary));
        }else {
            tvOne.setText("图片");
            tvTwo.setText("视频");
            tvTwo.setTextColor(ContextCompat.getColor(mContext, R.color.black_x));
        }

        tvOne.setOnClickListener(this);
        tvTwo.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (onItemClickListener != null) {
            if (view.getId() == R.id.tv_one) {
                onItemClickListener.onItemClick(mContext.getResources().getInteger(R.integer.pw_image_one), "");
                dismiss();
            } else if (view.getId() == R.id.tv_two) {
                onItemClickListener.onItemClick(mContext.getResources().getInteger(R.integer.pw_image_two), "");
                dismiss();
            } else {
                dismiss();
            }
        }
    }

}
